home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / mfwrite.zip / WRITE.C < prev    next >
Text File  |  1990-10-25  |  6KB  |  195 lines

  1. char *Copyright = "Copyright 1990, Geneva Switzerland, Markus G. Fischer";
  2.  
  3. char HELP[] =
  4. "write.com version 1.1, Markus G. Fischer, Oct. 1990\n"
  5. "\n"
  6. "Usage: write <string> \n"
  7. "       Outputs <string> to standard output, without a CR-LF sequence.\n"
  8. "       Meta-strings in the form $c are expanded much like in the `prompt'\n"
  9. "       (the differences are marked with a `*', unknown codes are skipped).\n"
  10. "\n"
  11. "       $  the `$' character                g  the `>' character\n"
  12. "     * t  time up to the seconds           l  the `<' character\n"
  13. "     * d  date without day of the week     b  the `|' character\n"
  14. "       p  current logical directory        q  the `=' character\n"
  15. "     * r  current physical directory       h  the backspace character\n"
  16. "       n  default drive letter             e  the escape character\n"
  17. "     * v  volume label                   * f  the form-feed character\n"
  18. "     * ^c CTRL-c (if '@' <= c <= '_')      _  the CR LF sequence";
  19.  
  20. /*
  21.  * Written for TurboC 1.5, Markus G. Fischer, January 1989
  22.  * Write *must* be compiled with the tiny memory model, and converted to *.com
  23.  * with EXE2BIN.  It doesn't read parameters through *argv[], but uses the true
  24.  * command line at CS:82 (hex), in order to get the white space correctly...
  25.  *
  26.  * November 1989, version 1.1:
  27.  * removed bug involving a terminal $, cosmetic changes, added $f $^c.
  28.  *
  29.  * October 1990:
  30.  * minor changes, released to the public domain.
  31.  *
  32.  * BUG: $^J sends a CR-LF, not just the LF
  33.  *
  34.  * Do whatever you like with this code, exept selling it, or pretending that
  35.  * you wrote it.  Send comments to "fisher@sc2a.unige.ch".
  36.  */
  37.  
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include <dos.h>
  43. #include <dir.h>
  44.  
  45. #define COMMAND_LINE MK_FP(_CS,0x82)
  46. #define CTRL(c)      (((toupper(c)&'\xe0')=='\x40')?toupper(c)&'\x1f':(c))
  47. #define putstr(s)    fputs(s,stdout)
  48.  
  49. char           path[MAXPATH] = "",
  50.                true_path[MAXPATH];
  51. struct country here;
  52.  
  53.  
  54. void getpath(void)
  55. /*
  56.  * Stores the current path into `path' and the true path into `true_path'.
  57.  * The true path is obtaind by the dos function call 0x60.
  58.  * DS:SI is input path; ES:DI is output path.
  59.  */
  60. {
  61.     union REGS r;
  62.     struct SREGS s;
  63.  
  64.     if (*path != '\0')   /* i.e. second call */
  65.         return;
  66.     getcwd (path, MAXPATH);
  67.     r.x.ax = 0x6000;
  68.     /* assuming near pointers: */
  69.     r.x.si = (unsigned)path;
  70.     r.x.di = (unsigned)true_path;
  71.     segread(&s);
  72.     s.es = s.ds;
  73.     intdosx(&r,&r,&s);
  74. }
  75.  
  76.  
  77. char *getvolume (void)
  78. /*
  79.  * Finds the volume label of the (physical) current drive.
  80.  * Detects when working on a JOIN'ed, SUBST'ed or network drive, and
  81.  * determines where to look for the volume.
  82.  * The detection of Network drives should be modified to suit other
  83.  * softwares.  Here, it is a path with a leading double-backslash (`\\')
  84.  * that indicates a network drive.
  85.  */
  86. {
  87.     static char volume[12];
  88.     char root[MAXPATH];
  89.     struct ffblk ffblk;
  90.     int len;
  91.  
  92.     getpath();
  93.     if (*path != *true_path && *true_path != '\\')
  94.     /* i.e. different drive letters */
  95.         if (strlen(true_path) > strlen(path))
  96.         /* i.e. SUBST */
  97.             strncpy (root, true_path, len=3);
  98.         else {
  99.         /* i.e. JOIN */
  100.             strncpy (root, path, len=strlen(path)-strlen(true_path)+3);
  101.             if (root[len-1] != '\\')
  102.                 root[len++] = '\\';
  103.             }
  104.     else
  105.     /* i.e. `normal' or network drive */
  106.        strncpy (root, path, len=3);
  107.     root[len] = '\0';
  108.     strcat (root, "*.*");
  109.     if (!findfirst (root, &ffblk, FA_LABEL)) {
  110.         strncpy (volume, ffblk.ff_name, 8);
  111.         if (strlen(ffblk.ff_name) > 9)
  112.             strncat (volume, &ffblk.ff_name[9], 3);
  113.         }
  114.     else
  115.         strcpy (volume, "(no volume)");
  116.     return (volume);
  117. } /* getvolume() */
  118.  
  119.  
  120. void getcountry(void)
  121. {
  122.     int static done = 0;
  123.  
  124.     if (done)
  125.         return;
  126.     country (0, &here);
  127.     done = 1;
  128. }
  129.  
  130.  
  131. void puts_date(void)
  132. {
  133.     struct date today;
  134.  
  135.     getdate (&today);
  136.     getcountry();
  137.     if (here.co_date == 2)   /* i.e. ASCII date format */
  138.         printf ("%d%s", today.da_year, here.co_dtsep);
  139.     if (here.co_date == 1)   /* i.e. european date format */
  140.         printf ("%02d%s%02d", today.da_day, here.co_dtsep, today.da_mon);
  141.     else                     /* i.e. non-european date format */
  142.         printf ("%02d%s%02d", today.da_mon, here.co_dtsep, today.da_day);
  143.     if (here.co_date != 2)   /* i.e. non-ASCII date format */
  144.         printf ("%s%d", here.co_dtsep, today.da_year);
  145. }
  146.  
  147.  
  148. void puts_time(void)
  149. {
  150.     struct time now;
  151.  
  152.     gettime (&now);
  153.     getcountry();
  154.     printf ("%2d%s%02d%s%02d",
  155.         now.ti_hour, here.co_tmsep, now.ti_min, here.co_tmsep, now.ti_sec);
  156. }
  157.  
  158.  
  159. void main (int argc)
  160. {
  161.     char far *command;
  162.  
  163.     if (argc == 1) {
  164.         puts (HELP);
  165.         exit (1);
  166.         }
  167.     for (command = COMMAND_LINE; *command != '\x0d'; command++) {
  168.         if (*command != '$')
  169.             putchar (*command);
  170.         else {
  171.             switch (tolower(*(++command))) {
  172.                 case '$': putchar ('$'); break;
  173.                 case 't': puts_time(); break;
  174.                 case 'd': puts_date(); break;
  175.                 case 'p': getpath(); putstr (path); break;
  176.                 case 'r': getpath(); putstr (true_path); break;
  177.                 case 'v': putstr (getvolume()); break;
  178.                 case 'n': putchar (getdisk()+'A'); break;
  179.                 case 'g': putchar ('>'); break;
  180.                 case 'l': putchar ('<'); break;
  181.                 case 'b': putchar ('|'); break;
  182.                 case 'q': putchar ('='); break;
  183.                 case 'h': putchar ('\x08'); break;   /* backspace */
  184.                 case 'e': putchar ('\x1b'); break;   /* esc */
  185.                 case 'f': putchar ('\x0c'); break;   /* form feed */
  186.                 case '_': puts (""); break;
  187.                 case '^': command++; putchar (CTRL(*command)); break;
  188.                 }
  189.             if (*command == '\x0d')
  190.                 command--;
  191.             }
  192.         } /* end command line parsing */
  193.     exit(0);
  194. } /* main() */
  195.